package org.lc.eic.synchronizer.requestdispatcher;


import org.lc.eic.api.requestreply.async.RequestDispatcher;
import org.lc.eic.core.pipesandfilters.Pipe;
import org.lc.eic.core.pipesandfilters.exception.ProcessingException;

/**
 * This is the replying system side in JUnit tests.
 * 
 * It receives a request and sends the same instance back as reply.
 * 
 * <pre>
 * 
 *       Change log:
 *       ------ --------------- ------------ --------------------------------------------
 *       Ver    By              Date         Description
 *       ------ --------------- ------------ --------------------------------------------
 *       1.0    Patrik Lycke    2008 mar 18  Created.
 * </pre>
 */
public class MockRequestDispatcher<I> implements Runnable, RequestDispatcher<I, I> {

	private MockBlockingPipe<I> mockBlockingPipe = new MockBlockingPipe<I>();
	
	private Pipe<I> replyPipe;

	private boolean running = true;

	public void run() {

		while (running) {

			try {
				replyPipe.process(mockBlockingPipe.take());
			} catch (ProcessingException e) {
				e.printStackTrace();

			}
		}

	}

	public void stop() {
		running = false;

		synchronized (this) {
			notifyAll();
		}
	}

	
	

	public Pipe<I> getRequestPipe() {		
		return mockBlockingPipe;
	}

	public void setReplyPipe(Pipe<I> replyPipe) {
		this.replyPipe=replyPipe;		
	}

	
}
